package com.m4399.shortcut_test

import android.app.Activity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import cn.m4399.openlibrary.oaid.OpenLibraryOaid
import com.github.gzuliyujiang.oaid.impl.r
import com.m4399.shortcut_test.core.helper.ShortcutHelper
import com.m4399.shortcut_test.setting.ShortcutPermission
import com.wzw.shortcuthelper.core.helper.CreateOrUpdateShortcut
import com.wzw.shortcuthelper.core.helper.PermissionRequest

/**
 * Description: TODO.
 *
 * @author  wang.zhiwei
 * @date    2024年07月31日 15:46
 *
 * Copyright (c) 2024年, 4399 Network CO.ltd. All Rights Reserved.
 */
class MainActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        OpenLibraryOaid.get(this){_,oaid->
            findViewById<TextView>(R.id.tvOaid).setText(oaid)

        }
        findViewById<Button>(R.id.btn1).setOnClickListener {
            PermissionRequest(this).start()
        }
        findViewById<Button>(R.id.btn2).setOnClickListener {
            if (CreateOrUpdateShortcut.isShortcutExit(this, "${packageName}_com.example.ApiTest", "apiTest")) {
                Toast.makeText(this, "已经创建过", Toast.LENGTH_SHORT).show()
                return@setOnClickListener

            }
            val check: Int = ShortcutPermission.check(this)
            Log.d("Shortcut", "onCreate: check $check")

            if (check == -1 || check == 3) {
                PermissionRequest(this).start()
            } else {
                ShortcutHelper.requestPinShortcut(
                    this, "com.example.ApiTest", "mainAct", ImageUtils.drawable2Bitmap(resources.getDrawable(applicationInfo.icon)),

                    "${packageName}_com.example.ApiTest",

                    "apiTest"
                )
            }
        }
    }

    override fun onRestart() {
        super.onRestart()
        val check: Int = ShortcutPermission.check(this)
        Log.d("Shortcut", "onRestart: $check")
        if (check != -1 && check != 3) {
            Toast.makeText(this, "已有权限 请手动添加", Toast.LENGTH_SHORT).show()
        }else{
            Toast.makeText(this, "没有权限权限 请手动授权", Toast.LENGTH_SHORT).show()
        }
    }
}